package com.facebank.usersupport.service.impl;

import com.facebank.usersupport.dto.CartDto;
import com.facebank.usersupport.dto.OrderDto;
import com.facebank.usersupport.mapper.usersupport.usersupport.*;
import com.facebank.usersupport.model.*;
import com.facebank.usersupport.service.IShopService;
import com.facebank.usersupport.util.GetOrder;
import com.facebank.usersupport.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Auther: yaozun
 * @Date: 2018/4/8 0008 09:25
 * @Description:
 */
@Service
public class ShopServiceImpl implements IShopService {
    @Autowired
    private DgutCartMapper dgutCartMapper;
    @Autowired
    private DgutOrderMapper dgutOrderMapper;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private AssAgentMapper assAgentMapper;
    @Autowired
    private DgutEvaluateMapper dgutEvaluateMapper;

    @Override
    public RestModel addCart(Integer actId, Integer adultNum, Integer childNum, HttpSession session) {
        Long assId = Long.valueOf(SessionUtil.getAssociatorId(session));
        DgutCart dgutCart = new DgutCart();
        dgutCart.setCreateTime(System.currentTimeMillis());
        int num = childNum+adultNum;
        dgutCart.setNumber(num);
        Activity activity = activityMapper.selectByPrimaryKey(actId);
        int total = childNum*activity.getChildPrice()+adultNum*activity.getAdultPrice();
        BigDecimal amountTotal = new BigDecimal(total);
        dgutCart.setAmountTotal(amountTotal);
        dgutCart.setAssociatorId(assId);
        dgutCart.setActivityId(actId);
        int is_success = dgutCartMapper.insertSelective(dgutCart);
        return new RestModel("加入购物车成功");
    }

    @Override
    public RestModel addOrder(Integer actId, Integer adultNum, Integer childNum, HttpSession session) {
        Long assId = Long.valueOf(SessionUtil.getAssociatorId(session));
        Long orderNo = GetOrder.genOrderId("2018");
        int num = childNum+adultNum;
        Activity activity = activityMapper.selectByPrimaryKey(actId);
        int total = childNum*activity.getChildPrice()+adultNum*activity.getAdultPrice();
        BigDecimal amountTotal = new BigDecimal(total);

        DgutOrder dgutOrder = new DgutOrder();
        dgutOrder.setAmountTotal(amountTotal);
        dgutOrder.setCreateTime(System.currentTimeMillis());
        dgutOrder.setNumber(num);
        dgutOrder.setOrderNo(orderNo);
        dgutOrderMapper.insertSelective(dgutOrder);

        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setOrderNo(orderNo);
        orderProduct.setActivityId(actId);
        orderProduct.setAssociatorId(assId);
        orderProduct.setCreateTime(System.currentTimeMillis());
        orderProductMapper.insertSelective(orderProduct);
        String str=new BigDecimal(orderNo.toString()).toString();
        return new RestModel(str);
    }

    @Override
    public RestModel payOder(Long orderNo,HttpSession session) {
        Long assId = Long.valueOf(SessionUtil.getAssociatorId(session));
        DgutOrder dgutOrder = dgutOrderMapper.selectByOrderNo(orderNo);
        dgutOrder.setOrderStatus((short)1);
        dgutOrder.setPayTime(System.currentTimeMillis());
        dgutOrderMapper.updateByPrimaryKeySelective(dgutOrder);

        AssAgent assAgent = assAgentMapper.selectByPrimaryKey(assId);
        BigDecimal preProfit =assAgent.getProfit();
        BigDecimal getProfit = dgutOrder.getAmountTotal().multiply(new BigDecimal(0.1));
        BigDecimal profit = preProfit.add(getProfit);
        assAgent.setProfit(profit);
        assAgentMapper.updateByPrimaryKeySelective(assAgent);
        return new RestModel("支出成功");
    }

    @Override
    public RestModel showCart(HttpSession session) {
        Long assId = Long.valueOf(SessionUtil.getAssociatorId(session));
        List<CartDto> cartDtos = dgutCartMapper.showCart(assId);
        return new RestModel(cartDtos);
    }

    @Override
    public RestModel showOrder(HttpSession session) {
        Long assId = Long.valueOf(SessionUtil.getAssociatorId(session));
        List<OrderDto> orderDtos = orderProductMapper.showOrderList(assId);
        return new RestModel(orderDtos);
    }

    @Override
    public RestModel balance(Long[] id) {
        for (Long i:id) {
            DgutCart dgutCart = dgutCartMapper.selectByPrimaryKey(i);
            Long orderNo = GetOrder.genOrderId("2018");
            DgutOrder dgutOrder = new DgutOrder();
            dgutOrder.setAmountTotal(dgutCart.getAmountTotal());
            dgutOrder.setCreateTime(System.currentTimeMillis());
            dgutOrder.setNumber(dgutCart.getNumber());
            dgutOrder.setOrderNo(orderNo);
            dgutOrder.setOrderStatus((short)1);
            dgutOrder.setPayTime(System.currentTimeMillis());
            dgutOrderMapper.insertSelective(dgutOrder);
            OrderProduct orderProduct = new OrderProduct();
            orderProduct.setActivityId(dgutCart.getActivityId());
            orderProduct.setAssociatorId(dgutCart.getAssociatorId());
            orderProduct.setCreateTime(System.currentTimeMillis());
            orderProduct.setOrderNo(orderNo);
            orderProductMapper.insertSelective(orderProduct);
            dgutCartMapper.deleteByPrimaryKey(i);
        }
        return new RestModel("結算成功");
    }

    @Override
    public RestModel evashowList(HttpSession session) {
        Long assId = Long.valueOf(SessionUtil.getAssociatorId(session));
        return new RestModel(dgutEvaluateMapper.showEvaluateList(assId));
    }

    @Override
    public RestModel addEvaluate(DgutEvaluate dgutEvaluate) {
        dgutEvaluate.setCreateTime(System.currentTimeMillis());
        dgutEvaluate.setStatus((byte)1);
        dgutEvaluateMapper.insertSelective(dgutEvaluate);
        return new RestModel("添加成功");
    }

    @Override
    public RestModel showOrderDetail(Long orderNo) {
        return new RestModel(orderProductMapper.showOrderDetail(orderNo));
    }

    //随机生成订单号
    public Long getNum19() {
        String numStr = "";
        String trandStr = String.valueOf((Math.random() * 9 + 1) * 1000000);
        String dataStr = new SimpleDateFormat("yyyyMMddHHMMSS").format(new Date());
        numStr = trandStr.toString().substring(0, 4);
        numStr = numStr + dataStr;
        return Long.valueOf(numStr);
    }
}
